package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsQrCodeInfoMapper;
import cc.rengu.igas.bsps.common.dao.UserInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsQrCodeInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.UserInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.QrCodeInfo;
import cc.rengu.igas.bsps.common.entity.UserInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.UserInfoModifyRequest;
import cc.rengu.igas.bsps.facade.response.UserInfoModifyResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 用户参数设置服务
 * 用户信息修改
 * Created by 王成 on 2018/12/3.
 * update by 赵奎伍 on 2020/04/06
 */
public class UserInfoModifyService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        UserInfoModifyRequest userInfoModifyRequest = new UserInfoModifyRequest();
        ConvertUtil.convertOutput(userInfoModifyRequest);
        return userInfoModifyRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        UserInfoModifyRequest requestParams = (UserInfoModifyRequest) request;

        /* 根据参数设置类型更新参数信息 */
        String instId = requestParams.getHeader().getInstId();
        String oprType = requestParams.getOprType();
        String modifyContent = requestParams.getModifyContent();
        String userId = requestParams.getUserId();
        if ("4".equals(oprType)) {
            if (StringUtil.isEmptyOrNull(userId)) {
                rglog.error("用户标识<{}>,修改内容类型<{}>及修改内容<{}>不能为空!", userId, oprType, modifyContent);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            /* 参数合法性校验 */
        } else if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(modifyContent) || StringUtil.isEmptyOrNull(oprType)) {
            rglog.error("用户标识<{}>,修改内容类型<{}>及修改内容<{}>不能为空!", userId, oprType, modifyContent);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        UserInfoMapper userInfoMapper = new UserInfoMapperImpl();
        UserInfo userInfo = userInfoMapper.selectBspUserInfoByPrimaryKey(instId, userId);
        if (null == userInfo) {
            rglog.error("用户<{}>信息不存在!", requestParams.getUserId());
            throw new BizException(RespCodeEnum.GET_USER_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_INFO_ERROR.getRespDesc());
        }
        switch (oprType) {
            case "0":
                userInfo.setUserImagePath(new String(Base64.decodeBase64(modifyContent)));
                rglog.info("更新用户 : 用户图像");
                break;
            case "1":
                userInfo.setNickName(modifyContent);
                rglog.info("更新用户 : 用户昵称");
                break;
            case "2":
                userInfo.setMobileNo(modifyContent);
                rglog.info("更新用户 : 用户电话");
                break;
            case "3":
                userInfo.setUserSex(modifyContent);
                rglog.info("更新用户 : 用户性别");
                break;
            case "4":
                userInfo.setEmail(modifyContent);
                rglog.info("更新用户 : 用户邮箱");
                break;
            default:
                rglog.error("不支持的参数设置类型:<{}>", oprType);
                throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
        }
        int iReturnCode = userInfoMapper.updateBspUserInfoByPrimaryKey(userInfo);
        if (0 != iReturnCode) {
            rglog.error("更新用户<{}>信息失败!信息类型<{}>,0-修改用户图像,1-修改用户昵称,2-修改用户电话,3-修改用户性别,4-修改用户邮箱", userInfo.getUserId(), oprType);
            throw new BizException(RespCodeEnum.UPDATE_USER_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_USER_INFO_ERROR.getRespDesc());
        }

        if ("0".equals(oprType)) {
            rglog.info("头像更新，无logo二维码也更新");
            BspsQrCodeInfoMapper bspsQrCodeInfoMapper = new BspsQrCodeInfoMapperImpl();
            List<QrCodeInfo> qrCodeInfoList = bspsQrCodeInfoMapper.selectBspsQrCodeInfoByUserId(instId, userId);
            if (!CollectionUtils.isEmpty(qrCodeInfoList)) {
                for (QrCodeInfo qrCodeInfo : qrCodeInfoList) {
                    String logoUrl = qrCodeInfo.getLogoUrlPath();
                    if (StringUtil.isEmptyOrNull(logoUrl)) {
                        logoUrl = new String(Base64.decodeBase64(modifyContent));
                        qrCodeInfo.setLogoUrlPath(logoUrl);
                        rglog.info("logo信息：" + logoUrl);
                        iReturnCode = bspsQrCodeInfoMapper.updateBspsQrCodeInfoById(qrCodeInfo);
                        if (0 != iReturnCode) {
                            rglog.error("更新用户二维码中心LOGO失败,qrCodeId:<{}>,iReturnCode:<{}>", qrCodeInfo.getId(), iReturnCode);
                        }
                    }
                    rglog.info("更新二维码logo完成");
                }
            }
        }

        /* 拼装应答报文 */
        UserInfoModifyResponse userInfoModifyResponse = new UserInfoModifyResponse();
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(userInfoModifyResponse);
        return bizResponse;
    }


    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        UserInfoModifyResponse userInfoModifyResponse = (UserInfoModifyResponse) bizResponse.getResult();
        ConvertUtil.convertInput(userInfoModifyResponse);
    }
}
